/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package 垃.圾;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import z.h.w.jar.kit.method.IO;
import z.h.w.jar.kit.method.Maker;
import z.h.w.jar.kit.method.To;

/**
 *
 * @author EnzoZhong
 */
public class Aoe extends AoeAbs {

	private Map<Long, File> longFileMap;

        /**
         * 
         * @param home
         */
        public Aoe(String home) {
		super(home);
		ini();
	}

        /**
         * 
         * @param home
         */
        public Aoe(File home) {
		super(home);
		ini();
	}

	private void ini() {
		longFileMap = new HashMap();
		File[] files = this.getHome().listFiles();
		for (File file : files) {
			Long id = Long.parseLong(IO.get(file, 0));
			longFileMap.put(id, file);
		}
	}

        /**
         * 
         * @param path
         * @param move
         * @return
         */
        @Override
	public Long add(String path, Boolean move) {
		return this.add(new File(path), move);
	}

        /**
         * 
         * @param from
         * @param move
         * @return
         */
        @Override//主方法
	public Long add(File from, Boolean move) {
		Long id;
		File[] files = this.getHome().listFiles();
		for (File f : files) {
			if (IO.equal(f, from)) {
				id = Long.parseLong(IO.get(f, 0));
				longFileMap.put(id, f);
				return id;
			}
		}
		id = Maker.makeLongId();
		String ext = IO.get(from, 1);
		File to = IO.newFile(this.getHome(), id + "." + ext);

		if (move) {
			IO.move(from, to);
		} else {
			IO.copy(from, to);
		}
		longFileMap.put(id, to);
		return id;
	}

        /**
         * 
         * @param files
         * @param move
         * @return
         */
        @Override
	public Long[] add(File[] files, Boolean move) {
		List<Long> list = new ArrayList();
		for (File file : files) {
			list.add(this.add(file, move));
		}
		return To.list2Array(list);
	}

        /**
         * 
         * @param file
         * @return
         */
        @Override
	public Long get(File file) {
		File[] files = this.getHome().listFiles();
		for (File f : files) {
			if (IO.equal(f, file)) {
				return Long.parseLong(IO.get(file, 0));
			}
		}
		return null;
	}

        /**
         * 
         * @param id
         * @return
         */
        @Override
	public File get(Long id) {
		return longFileMap.get(id);
	}

        /**
         * 
         * @param file
         * @return
         */
        @Override
	public Boolean has(File file) {
		File[] files = this.getHome().listFiles();
		for (File f : files) {
			if (IO.equal(f, file)) {
				return true;
			}
		}
		return false;
	}

        /**
         * 
         * @param id
         * @return
         */
        @Override
	public Boolean has(Long id) {
		return longFileMap.containsKey(id);
	}

        /**
         * 
         * @param file
         */
        @Override
	public void del(File file) {
		Long id = Long.parseLong(IO.get(file, 0));
		this.del(id);
	}

        /**
         * 
         * @param id
         */
        @Override
	public void del(Long id) {
		File file = this.get(id);
		if (file.delete()) {
			longFileMap.remove(id);
		}
	}
}
